//
//  ViewController.swift
//  BMICalculator
//
//  Created by YangJianyin on 2020/3/11.
//  Copyright © 2020 YangJianyin. All rights reserved.
//

import UIKit

class ViewController: UIViewController {
    @IBOutlet weak var inputLabal: UILabel!
    
    @IBOutlet weak var heightText: UITextField!
    
    @IBOutlet weak var weightText: UITextField!
    var height:Double?
    var weight:Double?
    
    @IBAction func computBtn(_ sender: UIButton) {
        print("computBtn click")
        height = Double(heightText.text!)
        weight = Double(weightText.text!)
        guard let height = height else {
            inputLabal.text = "输入错误，请重新输入"
            return;
        }
        guard let weight = weight else {
            inputLabal.text = "输入错误，请重新输入"
            return
        }
        inputLabal.text = BMICaculation(height: height, weight: weight);
    }
    func BMICaculation(height:Double,weight:Double) -> String {
        let BMI = weight/(height*height)
        let textValue = String(format: "%2.f", BMI)
        switch BMI {
        case 0.0..<18.5:
            return "您的BMI指数为\(textValue),情况过轻"
        case 18.5..<24.0:
            return "您的BMI指数为\(textValue)，情况正常"
        case 24.0..<28.0:
            return "您的BMI指数为\(textValue)，情况过重"
        case 24.0..<28.0:
            return "您的BMI指数为\(textValue)，情况过肥"
        default:
            return "输入错误，身高和体重不能为负数"
        }
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }


}

